﻿using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Admin.Controllers
{
    public class UserController : ApiController
    {
        [HttpGet]
        public IHttpActionResult user(string LoginName, int IsLockout = 2, int page = 1, int limit = 10)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var datalist = db.Users.AsEnumerable();
                if (!string.IsNullOrEmpty(LoginName))
                {
                    datalist = datalist.Where(p => p.LoginName.Contains(LoginName));
                }
                if (IsLockout == 0 || IsLockout == 1)
                {
                    datalist = datalist.Where(p => p.IsLockout == IsLockout);
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    LoginName = p.LoginName,
                    Password = p.Password,
                    IsLockout = p.IsLockout,
                    LastLoginTime = p.LastLoginTime.ToString(),
                    CreateTime = p.CreateTime.ToString(),
                    PsdWrongTime = p.PsdWrongTime,
                    LockTime = p.LockTime.ToString(),
                    PretectEmail = p.PretectEmail,
                    ProtectMtel = p.ProtectMtel
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult addUsers([FromBody]Users add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.LoginName == add.LoginName);
                if (list == null)
                {
                    add.CreateTime = DateTime.Now;
                    db.Users.Add(add);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "添加成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "添加失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "用户名不能重复！" });
                }

            }
        }
        [HttpPost]
        public IHttpActionResult GetZiXunShi()
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Roles.Select(p => new { Id = p.Id, Name = p.Name }).FirstOrDefault(p => p.Name == "咨询师");
                var list2 = db.UserRoles.Select(p => new { Id = p.Id, UserId = p.UserId, RoleId = p.RoleId }).Where(p => p.RoleId == list.Id).ToList();
                var list3 = db.Users.Select(p => new { Id = p.Id, LoginName = p.LoginName }).ToList();
                var list4 = db.Users.Select(p => new { Id = p.Id, LoginName = p.LoginName }).Where(p => p.Id == 0).ToList();
                foreach (var item in list2)
                {
                    foreach (var item2 in list3)
                    {
                        if (item.UserId == item2.Id)
                        {
                            list4.Add(item2);
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                return Json(list4);
            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody]Users add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.Id == add.Id);
                list.LoginName = add.LoginName;
                list.PretectEmail = add.PretectEmail;
                list.ProtectMtel = add.ProtectMtel;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult EditPwd([FromBody]Users add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.Id == add.Id);
                list.Password = "123456";
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Delete([FromBody]Users user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list1 = db.UserRoles.FirstOrDefault(p => p.UserId == user.Id);
                if (list1 == null)
                {
                    var list = db.Users.FirstOrDefault(p => p.Id == user.Id);
                    db.Users.Remove(list);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "该用户不能删除" });
                }

            }
        }
        [HttpGet]
        public IHttpActionResult GetRolesList()
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Roles.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name
                }).ToList();
                return Json(list);
            }
        }
        public IHttpActionResult showswitch([FromBody] Users user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.Id == user.Id);
                if (user.IsLockout == 0)
                {
                    list.IsLockout = user.IsLockout;
                    list.LockTime = null;
                }
                else
                {
                    list.IsLockout = user.IsLockout;
                    list.LockTime = DateTime.Now;
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult GetByIdList([FromBody]Roles rol)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Roles.Select(p => p.Id == rol.Id);
                return Json(list);
            }
        }
        [HttpPost]
        public IHttpActionResult GetRoleList([FromBody] Users u)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var data = db.Roles.Where(p => true);
                var datas = db.UserRoles.Where(p => p.UserId == u.Id).ToList();
                List<Roles> o = new List<Roles>();
                List<Roles> a = new List<Roles>();
                List<Roles> d = new List<Roles>();
                foreach (var i in data)
                {
                    o.Add(i);
                }
                foreach (var item in datas)
                {
                    var b = db.Roles.SingleOrDefault(p => p.Id == item.RoleId);
                    d.Add(b);
                }

                foreach (var item in d)
                {
                    foreach (var item2 in data)
                    {
                        if (item.Id == item2.Id)
                        {
                            o.Remove(item2);
                        }
                    }
                }

                // 未拥有
                var list = o.Distinct().Select(p => new
                {
                    p.Id,
                    p.Name
                }).ToList();

                // 已拥有
                var list2 = d.Distinct().Select(p => new
                {
                    p.Id,
                    p.Name
                }).ToList();
                return Json(new { data = list, datas = list2 });
            }
        }

        // 未完成
        public IHttpActionResult AddT([FromBody]lists lis)
        {
            using (CRMEntities db = new CRMEntities())
            {
                if (lis.str == "right")
                {
                    string[] intlist = lis.name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in intlist)
                    {
                        UserRoles a = new UserRoles();
                        a.UserId = lis.id;
                        a.RoleId = Convert.ToInt32(item);
                        db.UserRoles.Add(a);
                    }
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "修改ok" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "修改err" });
                    }
                }
                else if (lis.str == "left")
                {
                    string[] intlist = lis.name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in intlist)
                    {
                        var i = Convert.ToInt32(item);
                        var c = db.UserRoles.SingleOrDefault(p => p.RoleId == i && p.UserId == lis.id);
                        db.UserRoles.Remove(c);
                    }
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "修改ok" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "修改err" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "修改err" });
                }
            }
        }

    }
}
